
// 加载FORM表单扩展
layui.use(['form', 'laydate'], function(){
	var form = layui.form()
	,layer = layui.layer,
	laydate = layui.laydate;

	//自定义验证规则
	form.verify({

	});

	var lzd = function (data) {
		if (data == 1) {
            $("#startTime").show();
            $("#endTime").show();
            $("#timeList").hide();
            $("#setTime").hide();
		} else if (data == 2){
            $("#startTime").hide();
            $("#endTime").hide();
            $("#timeList").show();
            $("#setTime").show();
		}
    }
    var lzb = function (data) {
		if(data == 1){
			$('.is-set-end').css('display','block')
		}else{
            $('.is-set-end').css('display','none')
		}
    }
    var end_time_set = $('input[name="end_time_set"]:checked').val();
    lzb(end_time_set);

	var delivery_time_set = $('input[name="delivery_time_set"]:checked').val();
	lzd(delivery_time_set);

    form.on('radio(delivery_time_set)', function(data){
    	lzd(data.value);
    });
    form.on('radio(end_time_set)', function(data){
        lzb(data.value);
    });

	var getIds = function(){
		var ids = [];
		$('.listBox tr').each(function (index, item) {
            ids.push($(item).attr('data-id'));
        });
		return ids;
	};

	//监听提交
	form.on('submit(submit)', function(data){
		var URL = '/AdDelivery/add';
		var postData = {};
		var formData = data.field;
		var ids = getIds();
			ids = !!ids.length && ids.join(',') || '';
		postData['order_id']                   =   formData.o_id;
		postData['ad_name']                    =   formData.ad_name;
		postData['sale_mode']                  =   formData.sale_mode;
		postData['billing_mode']               =   formData.billing_mode;
		postData['price']                      =   formData.price;
		postData['carrier_type']               =   formData.carrier_type;
		postData['code_id']                    =   formData.code_id;
		postData['delivery_time_set']          =   formData.delivery_time_set;
		postData['start_time']                 =   (formData.startTime != "") 	? (formData.startTime + ' ' + formData.begin_time) : "";
		postData['end_time']                   =   (formData.end_time_set == 0) ? "" : ( (formData.endTime != "") 	? (formData.endTime + ' ' + formData.end_time) : "");
		postData['interval_time']              =   formData.interval_time;
		postData['delivery_id']                =   formData.delivery_id;
		postData['delivery_limit']             =   formData.delivery_limit;
		postData['daily_delivery_limit']       =   formData.daily_delivery_limit;
		postData['frequency_limit']            =   formData.frequency_limit;
        postData['end_time_set']			   =   formData.end_time_set;
        postData['ad_id']                      =   ids;

		if( typeof formData.d_id != 'undefined' ) {
			postData['id'] = formData.d_id;
			URL = '/AdDelivery/edit';
		}

		$.ajax({
		  type:"POST",
		  url:URL,
		  dataType:"json",
		  data:postData,
		  success: function(data){
			  if(data.status == 'success') {
				  PopModel.refreshParentFrame();
			  } else {
				layer.msg(data.msg, {icon: 5, shift: 6}, function(){});
			  }
			return false;
		  }
		});

		return false;
	});
    //监听select
    form.on('select(media)', function(data){
        var media_id =data.value;
        $.ajax({
            url: "/Code/MediaTocode",
            type: "post",
            data: {'id':media_id} ,
            dataType: "json",
            success: function (msg) {
                if(msg.status == 'success') {

                    $("#code_id").empty();
                    $("#code_id").append(msg.code);
                    form.render();
                } else {
                    console.log('有错误了!');
                }
            }
        });
    });
	// 刷新表单
	form.render();
});